package com.invengo.library_rfid.analyze.utils.uhf.formatHelp;

import com.invengo.library_rfid.analyze.utils.uhf.entity.STORED_MARK;

import java.util.ArrayList;
import java.util.List;

public class CContentIndex {
    private int _Data_Model;
    private List<COID> _OID_Array;
    private String _OID_bit_string;
    private int _user_data_read_length;

    public int getData_Model() {
        return _Data_Model;
    }

    public void setData_Model(int data_Model) {
        _Data_Model = data_Model;
    }

    public List<COID> getOID_Array() {
        return _OID_Array;
    }

    public void setOID_Array(List<COID> OID_Array) {
        _OID_Array = OID_Array;
    }

    public String getOID_bit_string() {
        return _OID_bit_string;
    }

    public void setOID_bit_string(String OID_bit_string) {
        _OID_bit_string = OID_bit_string;
    }

    public int getUser_data_read_length() {
        return _user_data_read_length;
    }

    public void setUser_data_read_length(int user_data_read_length) {
        _user_data_read_length = user_data_read_length;
    }

    public int getNeed_userdata_stored_count() {
        int count = 0;
        for (COID oid : _OID_Array) {
            if (oid.isStored() && oid.getStored_mark() == STORED_MARK.USER) {
                count++;
            }
        }
        return count;
    }

    public void SetContentIndex(String bitstring) {
        if (bitstring.length() > 16) {
            bitstring = bitstring.substring(0, 16);
        }
        if (bitstring.length() < 16) {
            bitstring = String.format("%1$" + 16 + "s", bitstring).replace(' ', '0');
        }
        _OID_bit_string = bitstring;
        for (int i = 0; i < 16; i++) {
            COID oid = _OID_Array.get(15 - i);
            oid.setStored(bitstring.substring(i, i + 1).equals("1"));
        }
    }

    public byte[] GetBytes() throws Exception {
        try {
            byte[] byteData = new byte[2];
            _OID_bit_string = "";
            for (int i = 0; i < 16; i++) {
                _OID_bit_string = (_OID_Array.get(i).isStored() ? "1" : "0") + _OID_bit_string;
            }
            byteData[0] = (byte) Integer.parseInt(_OID_bit_string.substring(0, 8), 2);
            byteData[1] = (byte) Integer.parseInt(_OID_bit_string.substring(8, 16), 2);
            return byteData;
        } catch (Exception ex) {
            throw new Exception("get contentindex bytes error:" + ex.getMessage());
        }
    }

    @Override
    public String toString() {
        String str = "";
        for (COID oid : _OID_Array) {
            if (oid.getStored_mark() == STORED_MARK.USER && oid.getOID_Value() != null) {
                str += oid.getOID_Index() + " " + oid.getOID_Name() + ":" + oid.getOID_Value() + "\r\n";
            }
        }
        return str;
    }

    public CContentIndex() {
        _Data_Model = 1;
        _user_data_read_length = 14;
        _OID_bit_string = String.format("%1$" + 16 + "s", "0").replace(' ', '0');
        _OID_Array = new ArrayList<>();
        COID oid = null;
        oid = new COID(3, "所属馆标识(Owner Library)", STORED_MARK.USER, false);
        oid.setOID_Length(2);
        _OID_Array.add(oid);
        oid = new COID(4, "卷册信息(Set Information)", STORED_MARK.USER, false);
        oid.setOID_Length(4);
        _OID_Array.add(oid);
        oid = new COID(5, "馆藏类别与状态(Type of Usage", STORED_MARK.DataBase, false);
        _OID_Array.add(oid);
        oid = new COID(6, "馆藏位置(Item Location) ", STORED_MARK.DataBase, false);
        _OID_Array.add(oid);
        oid = new COID(11, "馆际互借借入馆(IL Borrowing Library) ", STORED_MARK.USER, false);
        oid.setOID_Length(2);
        _OID_Array.add(oid);
        oid = new COID(12, "馆际互借事务号(ILL Borrowing Transaction Number) ", STORED_MARK.DataBase, false);
        _OID_Array.add(oid);
        oid = new COID(14, "备选的馆藏标识符(Alternative Item Identifier) ", STORED_MARK.USER, false);
        _OID_Array.add(oid);
        oid = new COID(15, "临时馆藏位置(Temporary Item Location)", STORED_MARK.DataBase, false);
        _OID_Array.add(oid);
        oid = new COID(16, "主题(Subject) ", STORED_MARK.DataBase, false);
        _OID_Array.add(oid);
        oid = new COID(24, "分馆标识(Subsidiary of an Owner Library) ", STORED_MARK.DataBase, false);
        _OID_Array.add(oid);
        oid = new COID(26, "国际标准书/刊号(ISBN/ISSN) ", STORED_MARK.DataBase, false);
        _OID_Array.add(oid);
        oid = new COID(27, "扩展保留位(Reserved Data) ", STORED_MARK.USER, false);
        _OID_Array.add(oid);
        oid = new COID(28, "扩展保留位(Reserved Data) ", STORED_MARK.USER, false);
        _OID_Array.add(oid);
        oid = new COID(29, "扩展保留位(Reserved Data) ", STORED_MARK.USER, false);
        _OID_Array.add(oid);
        oid = new COID(30, "扩展保留位(Reserved Data) ", STORED_MARK.USER, false);
        _OID_Array.add(oid);
        oid = new COID(31, "扩展保留位(Reserved Data) ", STORED_MARK.USER, false);
        _OID_Array.add(oid);
    }

    public byte[] GetBytes_User_Values() {
        if (_Data_Model == 1) {
            return GetBytes_DataModel_1();
        }
        return null;
    }

    public int Set_User_Values(byte[] bydata) throws Exception {
        int get_count = 1;
        try {
            CBitClass bitclass = new CBitClass(bydata);
            int start_bit_position = 0;
            while (start_bit_position < bydata.length * 8) {
                int oid_index = (int) bitclass.GetValue(start_bit_position, 6);
                if (oid_index <= 0) {
                    break;
                }
                int data_length = (int) bitclass.GetValue(start_bit_position + 6, 10);
                get_count++;
                start_bit_position += 16;
                if (data_length > 0) {
                    int index = _OID_Array.indexOf(oid_index);
                    if (index >= 0) {
                        switch (oid_index) {
                            case 3:
                                long lvalue = bitclass.GetValue(start_bit_position, data_length * 8);
                                if (String.valueOf(lvalue).length() == 5 && String.valueOf(lvalue).substring(0, 1).equals("6")) {
                                    lvalue = Long.parseLong("9" + String.valueOf(lvalue).substring(1));
                                }
                                _OID_Array.get(index).setOID_Value(lvalue);
                                break;
                            case 4:
                                int tmplength = data_length * 8 / 2;
                                long set_information_order = bitclass.GetValue(start_bit_position, tmplength);
                                long set_information_count = bitclass.GetValue(start_bit_position + tmplength, tmplength);
                                _OID_Array.get(index).setOID_Value(set_information_count + "/" + set_information_order);
                                break;
                            case 11:
                                lvalue = bitclass.GetValue(start_bit_position, data_length * 8);
                                if (String.valueOf(lvalue).length() == 5 && String.valueOf(lvalue).substring(0, 1).equals("6")) {
                                    lvalue = Long.parseLong("9" + String.valueOf(lvalue).substring(1));
                                }
                                _OID_Array.get(index).setOID_Value(lvalue);
                                break;
                        }
                    }
                }
                start_bit_position += data_length * 8;
            }
        } catch (Exception ex) {
            throw new Exception("set content index byte value error:" + ex.getMessage());
        }
        return get_count;
    }

    private byte[] GetBytes_DataModel_1() {
        CBitClass bitclass = new CBitClass(1024);
        for (COID oid : _OID_Array) {
            if (!oid.isStored() || oid.getStored_mark() != STORED_MARK.USER) {
                continue;
            }
            if (oid.getOID_Value() == null || oid.getOID_Length() <= 0 || oid.getOID_Length() > 128) {
                continue;
            }
            switch (oid.getOID_Index()) {
                case 3:
                    bitclass.AddBits(oid.getOID_Index(), 6);
                    bitclass.AddBits(oid.getOID_Length(), 10);
                    long lvalue = 0;
                    if (oid.getOID_Value() instanceof Long) {
                        lvalue = (Long) oid.getOID_Value();
                        if (String.valueOf(lvalue).length() == 5 && String.valueOf(lvalue).substring(0, 1).equals("9")) {
                            lvalue = Long.parseLong("6" + String.valueOf(lvalue).substring(1));
                        }
                    }
                    bitclass.AddBits(lvalue, oid.getOID_Length() * 8);
                    break;
                case 4:
                    bitclass.AddBits(oid.getOID_Index(), 6);
                    bitclass.AddBits(oid.getOID_Length(), 10);
                    String[] str = oid.getOID_Value().toString().split("/");
                    long set_information_count = 0;
                    long set_information_order = 0;
                    if (str.length >= 2) {
                        try {
                            set_information_count = Long.parseLong(str[0]);
                            set_information_order = Long.parseLong(str[1]);
                        } catch (NumberFormatException e) {
                            set_information_count = 0;
                            set_information_order = 0;
                        }
                    }
                    bitclass.AddBits(set_information_order, oid.getOID_Length() * 8 / 2);
                    bitclass.AddBits(set_information_count, oid.getOID_Length() * 8 / 2);
                    break;
                case 11:
                    bitclass.AddBits(oid.getOID_Index(), 6);
                    bitclass.AddBits(oid.getOID_Length(), 10);
                    lvalue = 0;
                    if (oid.getOID_Value() instanceof Long) {
                        lvalue = (Long) oid.getOID_Value();
                        if (String.valueOf(lvalue).length() == 5 && String.valueOf(lvalue).substring(0, 1).equals("9")) {
                            lvalue = Long.parseLong("6" + String.valueOf(lvalue).substring(1));
                        }
                    }
                    bitclass.AddBits(lvalue, oid.getOID_Length() * 8);
                    break;
            }
        }
        if (bitclass.writed_byte_length() > 0) {
            byte[] byte_value = new byte[bitclass.writed_byte_length()];
            for (int i = 0; i < bitclass.writed_byte_length(); i++) {
                byte_value[i] = bitclass.bytes_value()[i];
            }
            return byte_value;
        }
        return null;
    }

    public void Set_OID_Value(int oidindex, Object value) {
        for (COID oid : _OID_Array) {
            if (oid.getOID_Index() == oidindex) {
                oid.setOID_Value(value);
                break;
            }
        }
    }
}


